<?php

/**
 * clanarina actions.
 *
 * @package    dnevnik
 * @subpackage clanarina
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class clanarinaActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
     $skolas = array();
     $skola_dbs = DbFinder::from('Skola')->select(array('Id', 'Ime'))->find();
     foreach($skola_dbs as $skola_db)
       $skolas[$skola_db['Id']] = $skola_db['Ime'];
     if($request->isMethod('post'))
     {
        $roditeljs = DbFinder::from('Profil')->
                     innerJoin('Profil.Id', 'Roditelj.Id')->
                     innerJoin('Roditelj.Id', 'Ucenik.RoditeljId')->
                     withColumn('Roditelj.Id', 'RoditeljId')->
                     withColumn('Roditelj.Clanarina', 'Clanarina')->
                     withColumn('Roditelj.Kredit', 'Kredit')->
                     withColumn('Roditelj.ClanarinaTst', 'Tst')->
                     withColumn('Roditelj.ClanarinaSoc', 'Soc')->
                     withColumn('Roditelj.Ocene', 'Oce')->
                     withColumn('Roditelj.Izostanci', 'Izo')->
                     withColumn('Roditelj.Domaci', 'Dom')->
                     withColumn('Roditelj.Obavestenja', 'Obv')->
                     withColumn('Roditelj.ObaTelefona', 'Obt')->
                     withColumn('Roditelj.ImeMajka', 'ImeMajka')->
                     withColumn('Roditelj.PrezimeMajka', 'PrezimeMajka')->
                     withColumn('Roditelj.ImeStaratelj', 'ImeStaratelj')->
                     withColumn('Roditelj.PrezimeStaratelj', 'PrezimeStaratelj')->
                     withColumn('Roditelj.OtacPok', 'OtacPok')->
                     withColumn('Roditelj.MajkaPok', 'MajkaPok')->
                     withColumn('Ucenik.RedBr', 'RedBr')->
                     withColumn('Ucenik.Id', 'UcenikId')->
                     where('Ucenik.SkolaId', $request->getParameter('skola_id'));
        if($request->getParameter('razred_id') != '')
          $roditeljs = $roditeljs->where('Ucenik.RazredId', $request->getParameter('razred_id'));

        if($request->getParameter('prezime_oca') != '')
          $roditeljs = $roditeljs->where('Profil.Prezime', 'like', '%'.$request->getParameter('prezime_oca').'%');
        if($request->getParameter('ime_oca') != '')
          $roditeljs = $roditeljs->where('Profil.Ime', 'like', '%'.$request->getParameter('ime_oca').'%');
        if($request->getParameter('prezime_majka') != '')
          $roditeljs = $roditeljs->where('Roditelj.PrezimeMajka', 'like', '%'.$request->getParameter('prezime_majka').'%');
        if($request->getParameter('ime_majka') != '')
          $roditeljs = $roditeljs->where('Roditelj.ImeMajka', 'like', '%'.$request->getParameter('ime_majka').'%');

        $roditeljs = $roditeljs->orderBy('Ucenik.RedBr')->orderBy('Profil.Prezime')->orderBy('Profil.Ime');
        $roditeljs = $roditeljs->find();

        $uceniks = DbFinder::from('Profil')->
                   innerJoin('Profil.Id', 'Ucenik.Id')->
                   select(array('Profil.Id', 'Profil.Prezime', 'Profil.Ime'), sfModelFinder::SIMPLE)->
                   where('Ucenik.SkolaId', $request->getParameter('skola_id'));
        if($request->getParameter('razred_id') != '')
          $uceniks = $uceniks->where('Ucenik.RazredId', $request->getParameter('razred_id'));
        if($request->getParameter('prezime') != '')
          $uceniks = $uceniks->where('Profil.Prezime', 'like', '%'.$request->getParameter('prezime').'%');
        $uceniks = $uceniks->find();

        $uceniks_arr = array();
        foreach($uceniks as $ucenik)
          $uceniks_arr[$ucenik[0]] = $ucenik[1].' '.$ucenik[2];
     }

     else
     {
        $uceniks_arr = array();
        $roditeljs = array();
     }

     $this->skolas = $skolas;
     $this->roditeljs = $roditeljs;
     $this->uceniks_arr = $uceniks_arr;
  }

  public function executeClanarinaDo(sfWebRequest $request)
  {
     $datum = Functions::datumEn($request->getParameter('datum'));
     DbFinder::from('Roditelj')->where('Id', $request->getParameter('roditelj_id'))->set(array('Clanarina' => $datum));
     $uplata = new Uplata();
     $uplata->setIznos($request->getParameter('iznos'));
     $uplata->setIzvod($request->getParameter('izvod'));
     $uplata->setProfilId($request->getParameter('roditelj_id'));
     $uplata->setSvrha(UplataPeer::$svrha['Članarina']);
     $uplata->save();
     return $this->renderText($datum);
  }

  public function executeCredit(sfWebRequest $request)
  {
     $roditelj = DbFinder::from('Roditelj')->findPk($request->getParameter('roditelj_id'));
     $roditelj->setKredit($roditelj->getKredit() + $request->getParameter('iznos'));
     $roditelj->save();

     $uplata = new Uplata();
     $uplata->setIznos($request->getParameter('iznos'));
     $uplata->setIzvod($request->getParameter('izvod'));
     $uplata->setProfilId($request->getParameter('roditelj_id'));
     $uplata->setSvrha(UplataPeer::$svrha['Kredit']);
     $uplata->save();
     return $this->renderText($datum);
  }

  public function executeMobilni(sfWebRequest $request)
  {
     DbFinder::from('Profil')->where('Id', $request->getParameter('roditelj_id'))->set(array('Mobilni' => $request->getParameter('mob')));
     return $this->renderText($datum);
  }

  public function executeBool(sfWebRequest $request)
  {
     if($request->getParameter('tip') == 'tst')
     {
        $val = DbFinder::from('Roditelj')->select('ClanarinaTst')->findPk($request->getParameter('roditelj_id'));
        DbFinder::from('Roditelj')->where('Id', $request->getParameter('roditelj_id'))->set(array('ClanarinaTst' => ($val == 0) ? 1 : 0));
     }

     else if($request->getParameter('tip') == 'soc')
     {
        $val = DbFinder::from('Roditelj')->select('ClanarinaSoc')->findPk($request->getParameter('roditelj_id'));
        DbFinder::from('Roditelj')->where('Id', $request->getParameter('roditelj_id'))->set(array('ClanarinaSoc' => ($val == 0) ? 1 : 0));
     }

     else if($request->getParameter('tip') == 'oce')
     {
        $val = DbFinder::from('Roditelj')->select('Ocene')->findPk($request->getParameter('roditelj_id'));
        DbFinder::from('Roditelj')->where('Id', $request->getParameter('roditelj_id'))->set(array('Ocene' => ($val == 0) ? 1 : 0));
     }

     else if($request->getParameter('tip') == 'izo')
     {
        $val = DbFinder::from('Roditelj')->select('Izostanci')->findPk($request->getParameter('roditelj_id'));
        DbFinder::from('Roditelj')->where('Id', $request->getParameter('roditelj_id'))->set(array('Izostanci' => ($val == 0) ? 1 : 0));
     }

     else if($request->getParameter('tip') == 'dom')
     {
        $val = DbFinder::from('Roditelj')->select('Domaci')->findPk($request->getParameter('roditelj_id'));
        DbFinder::from('Roditelj')->where('Id', $request->getParameter('roditelj_id'))->set(array('Domaci' => ($val == 0) ? 1 : 0));
     }

     else if($request->getParameter('tip') == 'obv')
     {
        $val = DbFinder::from('Roditelj')->select('Obavestenja')->findPk($request->getParameter('roditelj_id'));
        DbFinder::from('Roditelj')->where('Id', $request->getParameter('roditelj_id'))->set(array('Obavestenja' => ($val == 0) ? 1 : 0));
     }

     else if($request->getParameter('tip') == 'obt')
     {
        $val = DbFinder::from('Roditelj')->select('ObaTelefona')->findPk($request->getParameter('roditelj_id'));
        DbFinder::from('Roditelj')->where('Id', $request->getParameter('roditelj_id'))->set(array('ObaTelefona' => ($val == 0) ? 1 : 0));
     }

     return $this->renderText('/images/'.(($val == 0) ? 'v' : 'x').'.gif');
  }

  public function executePrint(sfWebRequest $request)
  {
     $kor_podaci = new KorisnickiPodaci($request->getParameter('skola_id'));
     $pdf = $kor_podaci->pdfJedan(ProfilPeer::$tip['roditelj'], $request->getParameter('roditelj_id'));

     $this->getResponse()->setContentType('application/pdf');
     $this->getResponse()->setHttpHeader('Pragma', 'no-cache');
     $this->getResponse()->setHttpHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');

     $pdf->Output();
     return sfView::NONE;
  }

  public function executeBraca(sfWebRequest $request)
  {
     $saved = 0;
     if($request->isMethod('post'))
     {
         DbFinder::from('Ucenik')->where('Id', $request->getParameter('ucenik_id'))->set(array('RoditeljId' => $request->getParameter('roditelj')));
         $saved = 1;
     }

     $form = new sfForm();
     $form->setWidget('roditelj', new sfWidgetFormPropelChoice(array('model' => 'Roditelj',
                                                                        'add_empty' => true,
                                                                         'renderer_class' => 'sfWidgetFormPropelJQueryAutocompleter',
                                                                         'renderer_options' => array(
                                                                           'model' => 'Roditelj',
                                                                           'url'   => '/backend.php/roditelj/ajaxList?skola_id='.$request->getParameter('skola_id'),
                                                                         ))));

      $html = '<html>
                <head>
                  <script type="text/javascript" src="/js/jquery.js"></script>
                  <script type="text/javascript" src="/js/jquery-ui.js"></script>
                  <link rel="stylesheet" type="text/css" media="all" href="/sfFormExtraPlugin/css/jquery.autocompleter.css" />
                  <script type="text/javascript" src="/sfFormExtraPlugin/js/jquery.autocompleter.js"></script>
                  <style>
                    input { width:300px;}
                  </style
                </head>
                <body>';
      if($saved)
        $html .= 'Sačuvan';
      $html .= '<form method="post">';
      $html .= $form;
      $html .= '<button type="submit">Promeni</button></form>';
      $html .= '</body></html>';
      return $this->renderText($html);
  }

  public function executeUplate(sfWebRequest $request)
  {
      $ucenik = DbFinder::from('Ucenik')->findPk($request->getParameter('ucenik_id'));
      $uplatas = DbFinder::from('Uplata')->where('ProfilId', $ucenik->getRoditeljId())->orderBy('CreatedAt')->find();
      $svrhas = UplataPeer::getSvrhas();
      $html = '<html>
                <head>
                  <script type="text/javascript" src="/js/clanarina.js"></script>
                  <script type="text/javascript" src="/js/jquery.js"></script>
                  <style>
                    td {
                      padding: 2px;
                      text-align: center;
                    }
                  </style
                </head>
                <body>';
      $html .= '<table border="1" cellpadding="0" cellspacing="0">';

      $html .= '<tr>';
      $html .= '<td>Iznos</td>';
      $html .= '<td>Svrha</td>';
      $html .= '<td>Izvod</td>';
      $html .= '<td>Datum</td>';
      $html .= '<td>Izbriši</td>';
      $html .= '</tr>';

      foreach($uplatas as $uplata)
      {
         $html .= '<tr>';
         $html .= '<td>'.$uplata->getIznos().'</td>';
         $html .= '<td>'.$svrhas[$uplata->getSvrha()].'</td>';
         $html .= '<td>'.$uplata->getIzvod().'</td>';
         $html .= '<td>'.Functions::datumSr($uplata->getCreatedAt()).'</td>';
         $html .= '<td><a href="javascript: void(0)" onclick="brisiUplatu('.$uplata->getId().')">Izbriši</td>';
         $html .= '</tr>';
      }
      $html .= '</table>';

      $html .= '</body></html>';
      return $this->renderText($html);
  }

  public function executeBrisiUplatu(sfWebRequest $request)
  {
     $uplata = DbFinder::from('Uplata')->findPk($request->getParameter('uplata_id'));
     $uplata->delete();

     return $this->renderText('');
  }

}
